package com.syq.play.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import com.syq.play.AppContent;
import com.syq.play.R;
import com.syq.play.adapter.IndexAdapter;
import com.syq.play.api.MediaStoreApi;
import com.syq.play.db.entity.SongManager;
import com.syq.play.impl.MediaStoreImpl;
import com.syq.play.model.FolderModel;
import com.syq.play.service.IPlayer;
import com.syq.play.service.LocalService;
import com.syq.play.util.FileScanTool;
import com.syq.play.view.MyProgress;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class IndexActivity extends Activity{
	private static final String TAG="HomePageActivity";
	public static final String KEY_TITLE="key_title";
	public static final String KEY_TYPE="key_type";
	public static final int ALL_MEDIA=0;
	public static final int All_ARLIST=1;
	public static final int ALL_ALBUM=2;
	public static final int ALL_FOLDER=3;
	public static final int ALL_ARLIST_MEDIA=4;
	public static final int ALL_ALBUM_MEDIA=5;
	public static final int ALL_FOLDER_MEDIA=6;
	public static final int ALL_FAVOURITE_MEDIA=7;
	public static final int ALL_RECENT=8;
	private Context context;
	private GridView gridView;
	private IndexAdapter imageAdapter;
	private ImageView ivBack;
	private TextView tvTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_home_page);
		context=this;
		initUi();
		initListener();
		getData();
	}
	private void initUi(){
		tvTitle=(TextView)findViewById(R.id.tv_title);
		gridView=(GridView)findViewById(R.id.gridview);
		ivBack=(ImageView)findViewById(R.id.iv_back);
		ivBack.setVisibility(View.INVISIBLE);
	}
	private void initListener(){
		gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				if(imageAdapter.getCount()==1){
					if(position==0){
						updateData();
					}
				}else{
					FolderModel item=imageAdapter.getListData().get(position);
					Bundle data=new Bundle();
					data.putString(KEY_TITLE, item.getFolderName());
					switch(position){
					case 0:
						data.putInt(KEY_TYPE, MediaActivity.TYPE_ALL);
						SRPlayActivity.mActivityGroup.mStartActivity(MediaActivity.class, data);
						break;
					case 1:
						data.putInt(KEY_TYPE, FolderActivity.TYPE_ARTIST);
						SRPlayActivity.mActivityGroup.mStartActivity(FolderActivity.class, data);
						break;
					case 2:
						data.putInt(KEY_TYPE, FolderActivity.TYPE_ALBUM);
						SRPlayActivity.mActivityGroup.mStartActivity(FolderActivity.class, data);
						break;
					case 3:
						data.putInt(KEY_TYPE, FolderActivity.TYPE_FOLDER);
						SRPlayActivity.mActivityGroup.mStartActivity(FolderActivity.class, data);
						break;
					case 4:
						data.putInt(KEY_TYPE, MediaActivity.TYPE_FAVORITE);
						SRPlayActivity.mActivityGroup.mStartActivity(MediaActivity.class, data);
						break;
					case 5:
						data.putInt(KEY_TYPE, MediaActivity.TYPE_RECENT);
						SRPlayActivity.mActivityGroup.mStartActivity(MediaActivity.class, data);
						break;
					case 6:
						updateData();
						break;
					}
				}
			}
		});
	}
	/**
	 * 获取首页数据
	 */
	private void getData(){
		new AsyncTask<Void, Void, List<FolderModel>>() {
			@Override
			protected List<FolderModel> doInBackground(
					Void... params) {
				return AppContent.mFolder.getIndexFolder();
			}
			@Override
			protected void onPostExecute(List<FolderModel> result) {
				if(result!=null){
					if(imageAdapter==null){
						imageAdapter=new IndexAdapter(context, result);
						gridView.setAdapter(imageAdapter);
					}else{
						imageAdapter.updateListData(result);
					}
				}
			};
		}.execute();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			goBack();
			Log.i("syq", "onKeyDown……");
			return super.onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	}
	private void goBack(){
		IPlayer mPlayer=SRPlayActivity.mActivityGroup.mPlayer;
		try {
			if(mPlayer==null || !mPlayer.isPlaying()){
				stopService(new Intent(context, LocalService.class));
				finish();
				System.exit(0);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		
		
//		if(mp!=null && mp.isPlaying()){
//			Intent intent=new Intent();
//			intent.setAction(Intent.ACTION_MAIN);
//			intent.addCategory(Intent.CATEGORY_HOME);
//			startActivity(intent);
//		}else{
//			finish();
//			System.exit(0);
//		}
	}
	/**
	 * 刷新数据
	 */
	private void updateData(){
		new AsyncTask<Void, Void, Void>(){
			MyProgress mProgress = new MyProgress(context);
			FileScanTool fileScanTool=new FileScanTool();
			protected void onPreExecute() {
				gridView.setVisibility(View.GONE);
				mProgress.show();
			};
			@Override
			protected Void doInBackground(Void... params) {
				SongManager.getInstance().delInexistentSong();
				fileScanTool.scanFile(context, ".mp3", tvTitle);
				getData();
				return null;
			}
			protected void onPostExecute(Void result) {
				mProgress.dismiss();
				tvTitle.setText("SRPlay");
				gridView.setVisibility(View.VISIBLE);
				Toast.makeText(context, "共发现"+fileScanTool.fileStr.size()+"首歌曲", Toast.LENGTH_LONG).show();
			};
		}.execute();
	}
}
